package com.ynny.dao;

import com.ynny.bean.StudentBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository(value = "studentDao")
public class StudentDaoImpl implements StudentDao{

    @Autowired
    StudentBean studentBean;

    @Autowired
    JdbcTemplate JdbcTemplate;

    @Override
    public Integer addStudentCount(String number, String name, String grade, String clbum, String gender) {
        String sql = "insert into student_tb(number,name,grade,clbum,gender)" +
                " values(?,?,?,?,?)";
        return JdbcTemplate.update(sql,number,name,grade,clbum,gender);
    }

    @Override
    public Integer updateNameStudentCount(String number, String name, String grade, String clbum, String gender, String name1) {
        String sql = "update student_tb set name = ?, number = ?, grade = ?, clbum = ?, gender = ? where name = ?";
        return JdbcTemplate.update(sql,name,number,grade,clbum,gender,name1);
    }

    @Override
    public StudentBean selectById(Integer id) {
        String sql = "select * from student_tb where id = ?";
        StudentBean studentBean = JdbcTemplate.queryForObject(sql,
                new BeanPropertyRowMapper<>(StudentBean.class),id);
        return studentBean;
    }

    @Override
    public List<StudentBean> selectAllStudent() {
        String sql = "select * from student_tb";
        List<StudentBean> studentBean = JdbcTemplate.query(sql,
                new BeanPropertyRowMapper<>(StudentBean.class));
        return studentBean;
    }


}
